<?php
class ResourceFile extends Resource
{
	private $associatedFiles = array();
	
	public function getAssociatedFiles(){return $this->associatedFiles;}

	public function __construct($database, $resourceID)
	{
		$this->database = $database;
		parent::__construct($database, $resourceID);
		
		if ($resourceID != 0)
		{
/*			$sql = "SELECT * from Files WHERE ResourceIDP=".$this->resourceID;
//			echo $sql;
			$this->database->ExecuteSQL($sql);
			$row = $this->database->getRow();
			
			$this->fileName = $row['FSFileName'];
			$this->fileSize = $row['FSFileSize'];
			$this->fileType = $row['FSFileType'];
*/
			$sql = "SELECT * FROM Files WHERE ResourceIDP = ".$this->resourceID;
			$database = $this->database->Recopy();
			$database->ExecuteSQL($sql);
			$files = array();
			while($row = $database->getRow())
			{
				$aFile= array();
				$aFile['PhysicalFileNumber'] = $row['PhysicalFileNumber'];
				$aFile['FileName'] = $row['FSFileName'];
				$aFile['FileSize'] = $row['FSFileSize'];
				$aFile['FileType'] = $row['FSFileType'];
				array_push($files,$aFile);
			}
			$this->associatedFiles = $files;

		}
	}
	
	
	public function getDataHTML()
	{
		$hidden = new Hidden(); // just used for ViewFile so fileID can be passed by POST
		$hidden->setName("FileID");
		$html = $hidden->getHTML();
				
		$hidden = new Hidden();
		$hidden->setName("DBTableName");
		$html .= $hidden->getHTML();		
		
		$html .= "<p>";
		foreach($this->associatedFiles as $file)
		{
			$html .= "<a onClick=\"viewFile('".$file['PhysicalFileNumber']."','Files','".$this->resourceID."');\" href=\"javascript:viewFile('".$file['PhysicalFileNumber']."','Files','".$this->resourceID."');\">".$file['FileName']." (".$file['FileSize']." bytes)</a><br>";
		}
		$html = substr($html,0,-4);
		$html .= "</p>\n";
		return $html;
	}
	
	public function getFullHTML()
	{
		$html = "<span class=\"ListMainTitle\">".$this->title."</span>\n";
		$html .= "<p class=\"floatstop\" />\n";
		$html .= "<span class=\"ListDescription\">".$this->description."</span>\n";
		$html .= "<p class=\"floatstop\" />\n";
		
		return $html;
	}
}

?>